//
//  ReadViewController.m
//  第三组_阅读器
//
//  Created by My Mac on 2016/12/24.
//  Copyright © 2016年 My Mac. All rights reserved.
//

#import "ReadViewController.h"
#import "NSString+JJStringToSize.h"
@interface ReadViewController ()

@end

@implementation ReadViewController
static bool isUp;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTextView];
    [self setNavigationBarButtonAndToolBar];
}
-(void)initTextView
{
    CGRect screenFrame = [UIScreen mainScreen].bounds;
    //CGSize textSize = [_bookStr sizeOfTextWithMaxSize:CGSizeMake(screenFrame.size.width, MAXFLOAT) font:[UIFont italicSystemFontOfSize:20]];
    _textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, screenFrame.size.width, screenFrame.size.height-49)];
    [self.view addSubview:_textView];

    //设置textView的内容
    _textView.text = _bookStr;
    //设置字体大小
    _textView.font = [UIFont italicSystemFontOfSize:20];
    //字体颜色
    _textView.textColor = [UIColor blackColor];
    //背景颜色
    _textView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //设置textView不可编辑属性
    _textView.editable = NO;
    _textView.bounces = NO;
    //* 设置textView的总页数
    _allPage = (_textView.contentSize.height / _textView.frame.size.height) + 1;
    //当前页数为 1
    _currentPage = 1;
}
-(void)setNavigationBarButtonAndToolBar
{
    UIBarButtonItem * cancel = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(clickCancel)];
    self.navigationItem.leftBarButtonItem = cancel;
    //工具栏toolBar
    //获取屏幕的高度
    CGRect screenFrame = [UIScreen mainScreen].bounds;
    UIToolbar * toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, screenFrame.size.height-55, screenFrame.size.width, 55)];
    [self.view addSubview:toolBar];
    UIBarButtonItem * upButton = [[UIBarButtonItem alloc]initWithTitle:@"上一页" style:UIBarButtonItemStylePlain target:self action:@selector(lastPage)];
    UIBarButtonItem * downButton = [[UIBarButtonItem alloc]initWithTitle:@"下一页" style:UIBarButtonItemStylePlain target:self action:@selector(nextPage)];
    _showButton = [[UIBarButtonItem alloc]initWithTitle:[NSString stringWithFormat:@"%d/%d",_currentPage,_allPage] style:UIBarButtonItemStylePlain target:self action:nil];
    UIBarButtonItem * flexSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    [toolBar setItems:@[upButton,flexSpace,_showButton,flexSpace,downButton] animated:YES];
}
#pragma mark - 上一页的点击事件
-(void)lastPage{
    if (_currentPage == 1) {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"已经是第一页" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"OK", nil];
        [alert show];
        return;
    }
    isUp = YES;
    //如果不是第一页，当前页数减一就是上一页
    _currentPage = _currentPage - 1;
    [self setViewToUpOrDown];
}

#pragma mark - 下一页的点击事件
-(void)nextPage{
    if (_currentPage==_allPage) {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"已经是最后一页了" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:@"确认", nil];
        [alert show];
        return;
    }
    isUp = NO;
    _currentPage = _currentPage + 1;
    [self setViewToUpOrDown];
}
#pragma mark - 将翻转到上一页还是下一页封装为一个方法
-(void)setViewToUpOrDown{
    [UIView beginAnimations:nil context:nil];
    //翻页时间
    [UIView setAnimationDuration:1];
    //设置翻转到上一页的定位
    [_textView setContentOffset:CGPointMake(0, (_currentPage-1)*(_textView.frame.size.height)) animated:YES];
    
    [_showButton setTitle:[NSString stringWithFormat:@"%d/%d",_currentPage,_allPage]];
    [UIView commitAnimations];
    
    
    //清空视图
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    if(isUp==YES){
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:NO];
    } else {
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:NO];
    }
    [UIView commitAnimations];
}

-(void)clickCancel
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
